package com.suyuan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suyuan.dao.PigInMapper;
import com.suyuan.dao.PurchaseInfoMapper;
import com.suyuan.entity.PigIn;
import com.suyuan.entity.PigInExample;
import com.suyuan.entity.PurchaseInfo;
import com.suyuan.service.PigInService;
import com.suyuan.utils.DeleteRows;
import com.suyuan.web.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 生猪入场业务类
 */
@Service(value = "pigInService")
public class PigInServiceImpl implements PigInService {

    @Resource
    private PigInMapper pigInMapper;

    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;


    /**
     * 生猪入场
     * @param pigIn
     */
    public void addPigIn(PigIn pigIn) throws  Exception {
        //1.查询是否有生猪信息
        Integer pigID = pigIn.getPurGid();
        if(pigIn.getPurGid() < 0  ){
            throw new Exception();
        }
        PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(pigID);
        //1.1如果生猪信息表已经存在生猪信息，直接入场
        if(purchaseInfo != null){
            pigInMapper.insert(pigIn);
        }else{
            //1.2生猪信息不存在，先录入生猪信息，再将入库信息录入生猪入库信息表
            PurchaseInfo purchaseInfo2 = new PurchaseInfo();
            purchaseInfo2.setGid(pigID);
            purchaseInfo2.setCompid(pigIn.getComGid());
            purchaseInfo2.setPrcweight(pigIn.getInweight());
            purchaseInfo2.setPurcaddr(pigIn.getBuyplace());
            purchaseInfo2.setPurcname(pigIn.getReceivername());
            purchaseInfo2.setPurctime(pigIn.getIntime());
            purchaseInfo2.setPurcfeedtime(new Date());
            purchaseInfo2.setSupplierphone(pigIn.getSupplytel());
            //生成生猪ID+UUID后三十二位
            String sourceCode =""+pigID+"" +UUID.randomUUID().toString().replaceAll("-","").substring(16);
            purchaseInfo2.setSourcecode(sourceCode);
            //   purchaseInfo2.setPurclevel(pigIn.getGrade().to);
            purchaseInfo2.setSuppliername(pigIn.getSupplyname());
            //设置采购序列号
            purchaseInfo2.setPurcnumber(pigIn.getBuysn());
            purchaseInfoMapper.insert(purchaseInfo2);
            pigInMapper.insert(pigIn);
        }

    }

    /**
     * 查询所有生猪信息
     * @return
     */
    public List<PigIn> findAllPigIn() {
        PigInExample pigInExample = null;
        return pigInMapper.selectByExample(pigInExample);

    }




    /**
     * 分页多条件查询
     * @param beginTime
     * @param endTime
     * @param purGid
     * @param page
     * @return
     */
    public PageInfo inquiry(Date beginTime, Date endTime, Integer purGid,Page page){

        PigInExample pigInExample = new PigInExample();
        PigInExample.Criteria criteria = pigInExample.createCriteria();
        if(purGid != null){
            criteria.andPurGidEqualTo(purGid);
        }
        if(beginTime != null){
            criteria.andIntimeGreaterThan(beginTime);
        }
        if (endTime != null){
            criteria.andIntimeLessThan(endTime);
        }
        pigInExample.setOrderByClause("pur_Gid ASC");
        PageHelper.startPage(page.getPageNumber(),page.getPageSize());
        List<PigIn> pigList = pigInMapper.selectByExample(pigInExample);

        PageInfo pageInfo = new PageInfo(pigList);
        return pageInfo;
    }


    /**
     * 根据生猪入场信息ID查询生猪入场信息
     * @param
     * @return
     */
    public PigIn findByPID(Integer gid) {
        return pigInMapper.selectByPrimaryKey(gid);
    }

    public int deletePig(Integer id) {
        pigInMapper.deleteByPrimaryKey(id);
        return 1;
    }

    public Boolean checkPig(Integer id) {
        PurchaseInfo purchaseInfo = purchaseInfoMapper.selectByPrimaryKey(id);
        if(purchaseInfo != null){
            return true;
        }else{
            return null;
        }
    }

    /**
     * 更新(部分)生猪入场信息并回显
     * @param pigIn
     * @return
     */
    public PigIn updatePigIn(PigIn pigIn) {

        PigIn pigIn1 = pigInMapper.selectByPrimaryKey(pigIn.getGid());
        pigIn1.setWeightunit(pigIn.getWeightunit());
        pigIn1.setInweight(pigIn.getInweight());
        pigIn1.setIntime(pigIn.getIntime());
        pigIn1.setGrade(pigIn.getGrade());
        pigIn1.setBuyplace(pigIn.getBuyplace());
        pigIn1.setSupplyname(pigIn.getSupplyname());
        pigIn1.setSupplytel(pigIn.getSupplytel());
        pigIn1.setReceivername(pigIn.getReceivername());
        pigIn1.setBuysn(pigIn.getBuysn());
        pigInMapper.updateByPrimaryKey(pigIn1);
        return pigIn1;
    }

    /**
     * 删除生猪入场信息
     * @param selectRows
     * @return
     */
    @Transactional
    public Integer deletePigInInfo(String selectRows) {
        DeleteRows deleteRows = new DeleteRows();
        int [] deleteID = deleteRows.selectMore(selectRows);
        for (Integer ID : deleteID){
            pigInMapper.deleteByPrimaryKey(ID);
    }
        return 1;
    }
}
